package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.basic.mapper.StrategyMainMapper;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.MdmRailStrategyEntity;
import com.kaili.basic.pojo.entity.MdmStrategyMainEntity;
import com.kaili.basic.pojo.vo.MdmStrategyMainVO;
import com.kaili.basic.pojo.vo.MdmStrategySetVO;
import com.kaili.basic.service.*;
import com.kaili.common.util.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
* 策略管理主 service实现类
* @author:zhangjl
* @Time: 2019-07-24
* @Copyright: ©  杭州凯立通信有限公司 版权所有
* @Warning: 本内容仅限于公司内部传阅,禁止外泄或用于其它商业目的
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class StrategyMainServiceImpl extends ServiceImpl<StrategyMainMapper, MdmStrategyMainEntity>  implements IStrategyMainService {

    @Autowired
    IStrategyWifiService strategyWifiService;
    @Autowired
    IStrategyApnService strategyApnService;
    @Autowired
    IGlobalDetailService globalDetailService;
    @Autowired
    IStrategyUpgradeService strategyUpgradeService;
    @Autowired
    IStrategySiteService strategySiteService;
    @Autowired
    IMdmRailService railService;


    @Override
    public MdmStrategySetVO queryById(Integer id) {
        List<MdmStrategySetVO> list = baseMapper.queryById(new HashMap<String, Object>(2) {{put("id", id);}});
        if(list !=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<MdmStrategyMainVO> queryList(MdmStrategyMainByDto dto) {
        return baseMapper.queryList(ObjectUtils.objectToMap(dto));
    }

    @Override
    public Page<MdmStrategyMainVO> queryPageList(Page<MdmStrategyMainVO> page, MdmStrategyMainQueryDto dto) {
        return page.setRecords(baseMapper.queryPageList(page, ObjectUtils.objectToMap(dto)));
    }

    @Override
    public List<MdmStrategyMainVO> queryExportList(MdmStrategyMainQueryDto dto) {
        return baseMapper.queryPageList(ObjectUtils.objectToMap(dto));
    }


    @Override
    public boolean addSite(MdmStrategyMainSetAddDto dto) {

        //1、策略主表
        MdmStrategyMainEntity entity = new MdmStrategyMainEntity();
        BeanUtils.copyProperties(dto,entity);
        baseMapper.insert(entity);
        //2、中间表
        if(dto.getRailTime() !=null && dto.getRailTime().length >0) {
            for (Integer timeId : dto.getRailTime()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(timeId);
                railStrategy.setStrategyId(entity.getId());
                railStrategy.setRailType(1);//围栏类型 1：时间围栏 2：地理围栏
                railService.addRailStrategy(railStrategy);
            }
        }
        if(dto.getRailGeo() !=null && dto.getRailGeo().length >0) {
            for (Integer geoId : dto.getRailGeo()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(geoId);
                railStrategy.setStrategyId(entity.getId());
                railStrategy.setRailType(2);//围栏类型 1：时间围栏 2：地理围栏
                railService.addRailStrategy(railStrategy);
            }
        }
        //3、设备策略表
        strategySiteService.add(entity.getId(),dto);
        //6、APN设置表
        if (dto.getStrategySiteDetail() != null) {
            if ("1".equals(dto.getStrategySiteDetail().getIsEnable())) {
                strategyApnService.add(entity.getId(),dto);
            }
        }
        //4、WIFI配置表
        strategyWifiService.add(entity.getId(),dto);
        //5、复选数据表
        globalDetailService.add(entity.getId(),dto);

        return true;
    }

    @Override
    public boolean addApply(MdmStrategyMainApplyAddDto dto) {

        //1、策略主表
        MdmStrategyMainEntity entity = new MdmStrategyMainEntity();
        BeanUtils.copyProperties(dto,entity);
        baseMapper.insert(entity);
        //2、中间表
        if(dto.getRailTime() !=null && dto.getRailTime().length >0) {
            for (Integer timeId : dto.getRailTime()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(timeId);
                railStrategy.setStrategyId(entity.getId());
                railStrategy.setRailType(1);//围栏类型 1：时间围栏 2：地理围栏
                railService.addRailStrategy(railStrategy);
            }
        }
        if(dto.getRailGeo() !=null && dto.getRailGeo().length >0) {
            for (Integer geoId : dto.getRailGeo()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(geoId);
                railStrategy.setStrategyId(entity.getId());
                railStrategy.setRailType(2);//围栏类型 1：时间围栏 2：地理围栏
                railService.addRailStrategy(railStrategy);
            }
        }
        if(dto.getFileId() !=null && dto.getFileId().length >0) {
            for (Integer fileId : dto.getFileId()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(fileId);
                railStrategy.setStrategyId(entity.getId());
                railStrategy.setRailType(3);//围栏类型 1：时间围栏 2：地理围栏 3:应用名
                railService.addRailStrategy(railStrategy);
            }
        }
        //3、复选数据表
        globalDetailService.addApply(entity.getId(),dto);

        return true;
    }

    @Override
    public boolean addUpgrade(MdmStrategyMainUpgradeAddDto dto) {

        //1、策略主表
        MdmStrategyMainEntity entity = new MdmStrategyMainEntity();
        BeanUtils.copyProperties(dto,entity);
        baseMapper.insert(entity);
        //2、中间表
        if(dto.getRailTime() !=null && dto.getRailTime().length >0) {
            for (Integer timeId : dto.getRailTime()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(timeId);
                railStrategy.setStrategyId(entity.getId());
                railStrategy.setRailType(1);//围栏类型 1：时间围栏 2：地理围栏
                railService.addRailStrategy(railStrategy);
            }
        }
        if(dto.getRailGeo() !=null && dto.getRailGeo().length >0) {
            for (Integer geoId : dto.getRailGeo()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(geoId);
                railStrategy.setStrategyId(entity.getId());
                railStrategy.setRailType(2);//围栏类型 1：时间围栏 2：地理围栏
                railService.addRailStrategy(railStrategy);
            }
        }
        if(dto.getFileId() !=null && dto.getFileId().length >0) {
            for (Integer fileId : dto.getFileId()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(fileId);
                railStrategy.setStrategyId(entity.getId());
                railStrategy.setRailType(4);//围栏类型 1：时间围栏 2：地理围栏 4:文件名
                railService.addRailStrategy(railStrategy);
            }
        }
        //3、升级类策略表
        strategyUpgradeService.add(entity.getId(),dto);

        return true;
    }

    @Override
    public boolean modify(MdmStrategyMainSetUpdateDto dto) {
        //1、策略主表
        MdmStrategyMainEntity entity = new MdmStrategyMainEntity();
        BeanUtils.copyProperties(dto,entity);
        baseMapper.updateById(entity);
        //2、中间表
        railService.deleteStrategyRail(entity.getId());
        if(dto.getRailTime() !=null && dto.getRailTime().length >0) {
            for (Integer timeId : dto.getRailTime()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(timeId);
                railStrategy.setStrategyId(entity.getId());
                railStrategy.setRailType(1);//围栏类型 1：时间围栏 2：地理围栏
                railService.addRailStrategy(railStrategy);
            }
        }
        if(dto.getRailGeo() !=null && dto.getRailGeo().length >0) {
            for (Integer geoId : dto.getRailGeo()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(geoId);
                railStrategy.setStrategyId(entity.getId());
                railStrategy.setRailType(2);//围栏类型 1：时间围栏 2：地理围栏
                railService.addRailStrategy(railStrategy);
            }
        }
        //3、设备类策略表
        strategySiteService.modify(entity);
        //6、APN设置表
        if (dto.getStrategySiteDetail() != null) {
            if ("1".equals(dto.getStrategySiteDetail().getIsEnable())) {
                strategyApnService.modify(entity);
            }
        }
        //4、WIFI配置表
        strategyWifiService.modify(entity);
        //5、复选数据表
        globalDetailService.modify(entity,dto.getGlobalClobList());

        return true;
    }

    @Override
    public boolean modifyApply(MdmStrategyMainApplyUpdateDto dto) {
        //1、策略主表
        MdmStrategyMainEntity entity = new MdmStrategyMainEntity();
        BeanUtils.copyProperties(dto,entity);
        baseMapper.updateById(entity);
        //2、中间表
        railService.deleteStrategyRail(entity.getId());
        if(dto.getRailTime() !=null && dto.getRailTime().length >0) {
            for (Integer timeId : dto.getRailTime()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(timeId);
                railStrategy.setStrategyId(entity.getId());
                railStrategy.setRailType(1);//围栏类型 1：时间围栏 2：地理围栏
                railService.addRailStrategy(railStrategy);
            }
        }
        if(dto.getRailGeo() !=null && dto.getRailGeo().length >0) {
            for (Integer geoId : dto.getRailGeo()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(geoId);
                railStrategy.setStrategyId(entity.getId());
                railStrategy.setRailType(2);//围栏类型 1：时间围栏 2：地理围栏
                railService.addRailStrategy(railStrategy);
            }
        }
        if(dto.getFileId() !=null && dto.getFileId().length >0) {
            for (Integer fileId : dto.getFileId()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(fileId);
                railStrategy.setStrategyId(entity.getId());
                railStrategy.setRailType(3);//围栏类型 1：时间围栏 2：地理围栏 3：应用名
                railService.addRailStrategy(railStrategy);
            }
        }
        //3、复选数据表
        globalDetailService.modify(entity,dto.getGlobalClobList());

        return true;
    }

    @Override
    public boolean modifyUpgrade(MdmStrategyMainUpgradeupdateDto dto) {
        //1、策略主表
        MdmStrategyMainEntity entity = new MdmStrategyMainEntity();
        BeanUtils.copyProperties(dto,entity);
        baseMapper.updateById(entity);
        //2、中间表
        railService.deleteStrategyRail(entity.getId());
        if(dto.getRailTime() !=null && dto.getRailTime().length >0) {
            for (Integer timeId : dto.getRailTime()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(timeId);
                railStrategy.setStrategyId(entity.getId());
                railStrategy.setRailType(1);//围栏类型 1：时间围栏 2：地理围栏
                railService.addRailStrategy(railStrategy);
            }
        }
        if(dto.getRailGeo() !=null && dto.getRailGeo().length >0) {
            for (Integer geoId : dto.getRailGeo()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(geoId);
                railStrategy.setStrategyId(entity.getId());
                railStrategy.setRailType(2);//围栏类型 1：时间围栏 2：地理围栏
                railService.addRailStrategy(railStrategy);
            }
        }
        if(dto.getFileId() !=null && dto.getFileId().length >0) {
            for (Integer fileId : dto.getFileId()) {
                MdmRailStrategyEntity railStrategy = new MdmRailStrategyEntity();
                railStrategy.setRailId(fileId);
                railStrategy.setStrategyId(entity.getId());
                railStrategy.setRailType(4);//围栏类型 1：时间围栏 2：地理围栏 4：文件名
                railService.addRailStrategy(railStrategy);
            }
        }
        //3、升级类策略表
        strategyUpgradeService.modify(entity, dto.getStrategyUpgradeDetail());

        return true;
    }

    @Override
    public Boolean updateStatus(List<Integer> idList, Integer status) {
        if (baseMapper.updateStatus(idList, status) > 0){
            return true;
        }

        return false;
    }

    @Override
    public List<MdmStrategySetVO> querySet(MdmStrategyMainByDto dto) {
        return baseMapper.querySet(ObjectUtils.objectToMap(dto));
    }

}
